

//返回功能
function fanHui0() {
    history.back();
}

//关闭弹出框功能
function guanBi(){
    promptbox.style.display = "none";
    promptbox.innerHTML = "";
}

//清空手机栏功能

chaCha.onclick = function () {
    num1.value = "";
};

//验证手机号格式是否正确
var phoneReg = /^1[345789]\d{9}$/;
num1.onblur = function(){
    if(num1.value==""){
        promptbox.style.display = "block";
        promptbox.innerHTML = "手机号不能为空！";
        setTimeout(guanBi,2000);
    }else{
        if(!phoneReg.test(num1.value)){
            promptbox.style.display = "block";
            promptbox.innerHTML = "手机号格式有误！";
            setTimeout(guanBi,2000);
        }
    }
    checkAll();
};
num1.onkeyup = function () {
    if(num1.value!=""){
        chaCha.src = "images/删除.png";
    }else{
        chaCha.src = "";
    }
    if(!phoneReg.test(num1.value)){
        yanzhenbtn.setAttribute("disabled","disabled");
        if(num1.value.length == 11){
            promptbox.style.display = "block";
            promptbox.innerHTML = "手机号格式有误！";
            setTimeout(guanBi,2000);
        }
    }else{
        if(num1.value.length == 11) {
            checkephone();
        }
    }
    checkAll();
};

//验证数据库是否有该手机号
function checkephone() {
    $.get("http://test.hfjava.com/shop/userphone?phone="+ num1.value,
        {},
        function (data) {
            if(data == "true"){
                promptbox.style.display = "block";
                promptbox.innerHTML = "该手机号未注册！";
                yanzhenbtn.setAttribute("disabled","disabled");
                setTimeout(guanBi,2000);
            }else{
                if(yanzhenbtn.value == "发送验证码"){
                    yanzhenbtn.removeAttribute("disabled");
                }
            }
        }
    );
}

//倒计时
var timer;
var t = 60;
var yzmReg = /^\d{6}/;
//倒计时开始
yanzhenbtn.onclick = function () {
    yanzhenbtn.setAttribute("disabled","disabled");
    yanzhenbtn.value = "重新发送60";
    timer = setInterval(daojishi,1000);
};
function daojishi() {
    t--;
    if(t==0){
        clearInterval(timer);
        yanzhenbtn.value = "发送验证码";
        yanzhenbtn.removeAttribute("disabled");
        t = 60 ;
        return;
    }
    yanzhenbtn.value = "重新发送"+t;
}
//倒计时结束
//验证验证码正确性
num2.onblur = function (){
    if(num2.value ==""){
        promptbox.style.display = "block";
        promptbox.innerHTML = "验证码不能为空！";
        setTimeout(guanBi,2000);
    }else{
        if(!yzmReg.test(num2.value)){
            promptbox.style.display = "block";
            promptbox.innerHTML = "验证码格式有误！";
            setTimeout(guanBi,2000);
        }
    }
    checkAll();

};
num2.onkeyup = function(){
    if(num2.value.length == 6){
        if(!yzmReg.test(num2.value)){
            promptbox.style.display = "block";
            promptbox.innerHTML = "验证码格式有误！";
            isYZMRight = false;
            setTimeout(guanBi,2000);
        }else{
            $.ajax({
                url:"http://test.hfjava.com/shop/yzm",
                dataType:"jsonp",
                success:function (data) {
                    if(data.code != num2.value){
                        promptbox.style.display = "block";
                        promptbox.innerHTML = "验证码有误！";
                        isYZMRight = false;
                        setTimeout(guanBi,2000);
                    }
                    isYZMRight = true;
                }
            });
        }
        checkAll();
    }
};



//密码栏js
var pwdReg0 = /^.{6,16}$/;
var pwdReg1 = /\d+/;
var pwdReg2 = /[a-zA-Z]+/;
var pwdReg3 = /[=~!@#\$%^&\*\(\)_\+\{\}\]:"\|',<?.\/\\]+/;
pwd1.onkeyup = function (){
    var x = pwd1.value;
    var rightCount = 0;
    var lengthValid = pwdReg0.test(x);
    var numberValid = pwdReg1.test(x);
    var letterValid = pwdReg2.test(x);
    var symbolValid = pwdReg3.test(x);
    if(x.length>= 6){
        if(numberValid){
            rightCount++;
        }
        if(letterValid){
            rightCount++;
        }
        if(symbolValid){
            rightCount++;
        }
        if(!lengthValid||rightCount<2){
            isPsw1Right = false;
        }else{
            isPsw1Right = true;

        }
    }else{
        if(!x.value){
            isPsw1Right = false;
        }
    }
    checkAll();
};
pwd1.onblur = function () {
    var x = pwd1.value;
    var rightCount = 0;
    var lengthValid = pwdReg0.test(x);
    var numberValid = pwdReg1.test(x);
    var letterValid = pwdReg2.test(x);
    var symbolValid = pwdReg3.test(x);
    if(!x){
        promptbox.style.display = "block";
        promptbox.innerHTML = "密码不能为空！";
        isPsw1Right = false;
        setTimeout(guanBi,2000);
    }else{
        if(numberValid){
            rightCount++;
        }
        if(letterValid){
            rightCount++;
        }
        if(symbolValid){
            rightCount++;
        }
        if(!lengthValid||rightCount<2){
            promptbox.style.display = "block";
            promptbox.innerHTML = "密码格式有误！";
            isPsw1Right = false;
            setTimeout(guanBi,2000);
            return;
        }else{
            isPsw1Right = true;

        }

    }

    checkAll();

};
//当密码栏获取焦点
//检测是否可以重置密码
var isYZMRight = false;
var isPsw1Right = false;
function checkAll() {
    var isRight = isYZMRight&&isPsw1Right;
    if(isRight){
        loginbtn.removeAttribute("disabled");
    }else{
        loginbtn.setAttribute("disabled","disabled");
    }
}
//重置密码

loginbtn.onclick = function () {
    setNewPwd();
};
function setNewPwd(){
    $.post("http://test.hfjava.com/shop/updateuserpwd",
        {phone:num1.value, newPwd:pwd1.value},
        function(data){
            if(data=="Saved"){
                promptbox.style.display = "block";
                promptbox.innerHTML = "注册成功！" ;
                setTimeout(deLu,2000);
            }
        });
}
//跳转到登录页
function deLu() {
    document.location.href ="loginpage.html";
}



